package org.linlinjava.litemall.db.service;

import org.linlinjava.litemall.db.dao.LitemallDistributionRuleMapper;
import org.linlinjava.litemall.db.domain.LitemallDistributionRule;
import org.linlinjava.litemall.db.domain.LitemallDistributionRuleExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Administrator
 */
@Service
public class LitemallDistributionRuleService {

    @Autowired
    private LitemallDistributionRuleMapper distributionRuleMapper;

    public void add(LitemallDistributionRule distributionRule) {
        distributionRule.setAddTime(LocalDateTime.now());
        distributionRule.setUpdateTime(LocalDateTime.now());
        distributionRuleMapper.insertSelective(distributionRule);
        if (distributionRule.getActive()) {
            throw new IllegalArgumentException("分销规则只能生效一个");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(LitemallDistributionRule distributionRule) {
        distributionRule.setUpdateTime(LocalDateTime.now());
        distributionRuleMapper.updateByPrimaryKeySelective(distributionRule);
        if (distributionRule.getActive()) {
            throw new IllegalArgumentException("分销规则只能生效一个");
        }
    }

    public LitemallDistributionRule save(LitemallDistributionRule distributionRule) {
        LitemallDistributionRule queryByMemberRule = queryByMemberRule(distributionRule.getMemberRuleId());
        if (queryByMemberRule != null) {
            queryByMemberRule.setDetail(distributionRule.getDetail());
            queryByMemberRule.setType(distributionRule.getType());
            queryByMemberRule.setActive(true);

            distributionRule.setUpdateTime(LocalDateTime.now());
            distributionRuleMapper.updateByPrimaryKeySelective(distributionRule);

        } else {
            distributionRule.setAddTime(LocalDateTime.now());
            distributionRule.setUpdateTime(LocalDateTime.now());
            distributionRuleMapper.insert(distributionRule);
        }
        return distributionRule;
    }

    public int delete(Integer id) {
        return distributionRuleMapper.deleteByPrimaryKey(id);
    }

    public LitemallDistributionRule list(Integer type, Integer memberRuleId) {
        LitemallDistributionRuleExample example = new LitemallDistributionRuleExample();
        LitemallDistributionRuleExample.Criteria criteria = example.or();
        criteria.andTypeEqualTo(type).andActiveEqualTo(true);
        if (!memberRuleId.equals(0)) {
            criteria.andMemberRuleIdEqualTo(memberRuleId);
        }
        LitemallDistributionRule litemallDistributionRule = distributionRuleMapper.selectOneByExample(example);
        return litemallDistributionRule;
    }

    public List<LitemallDistributionRule> listActive() {
        LitemallDistributionRuleExample example = new LitemallDistributionRuleExample();
        example.or().andActiveEqualTo(true);
        return distributionRuleMapper.selectByExample(example);
    }

    public LitemallDistributionRule findById(Integer id) {
        return distributionRuleMapper.selectByPrimaryKey(id);
    }

    public LitemallDistributionRule queryActive() {
        LitemallDistributionRuleExample example = new LitemallDistributionRuleExample();
        example.or().andActiveEqualTo(true);
        return distributionRuleMapper.selectOneByExample(example);
    }

    public LitemallDistributionRule queryByMemberRuleId(Integer ruleId) {
        LitemallDistributionRuleExample example = new LitemallDistributionRuleExample();
        example.or().andMemberRuleIdEqualTo(ruleId).andActiveEqualTo(true);
        return distributionRuleMapper.selectOneByExample(example);
    }

    public LitemallDistributionRule queryByMemberRule(Integer ruleId) {
        LitemallDistributionRuleExample example = new LitemallDistributionRuleExample();
        example.or().andMemberRuleIdEqualTo(ruleId);
        return distributionRuleMapper.selectOneByExample(example);
    }


}
